const app = getApp()
import utils from "../../utils/index";
import {
  userModel,
  fansModel
} from "../../apis/index"
import {
  InterAd
} from '../../utils/InterAd'
import regeneratorRuntime from '../../libs/wxPromise.min';

Page({
  data: {
    gradeList: [],
    page1: {
      page: 1,
      pageSize: 20
    },
    myFans: [],
  },
  mixins: [require('../../myMixin.js')],
  async onLoad() {
    getApp().setWatcher(this)

  },
  onReady: function () {

    let interAd = new InterAd('7fb828e435af61d59234ae3978c20221')
    interAd.showAd()

  },
  async getUserInfo() {
    let check = await userModel.haveUserInfo()
    if (check.data) {
      let user = await userModel.getUserInfo()
      this.setData({
        userInfo: user
      }, () => {
        app.globalData.userInfo = user
      })
    } else {
      this.setData({
        modalName: 'Auth'
      })
    }
  },
  onShow() {
    this.getUserInfo()
    this.getMyFansList()
    this.getFansGrade()
    this.haveGetStar()
    this.setData({
      isIOS: app.globalData.isIOS,
      isShow: app.globalData.isShow
    })
  },
  haveGetStar() {
    let now = new Date()
    let year = now.getFullYear()
    let month = now.getMonth()
    let date = now.getDate()
    let key = `${year}${month}${date}`
    let info = utils.prefix.getStorageSync(key) || '';
    this.setData({
      gotStar: info ? true : false
    })
  },
  onGetUserInfo(e) {
    if (e.detail.errMsg == 'getUserInfo:ok') {
      let {
        avatarUrl,
        nickName
      } = e.detail.userInfo
      userModel.saveUserInfo({
        avatar: avatarUrl,
        name: nickName
      }).then(res => {
        this.setData({
          modalName: ''
        }, () => {
          this.getUserInfo()
        })
      })
    }
  },
  viewClubDetail(e) {
    let item = e.currentTarget.dataset.item
    utils.goUrl(`../supstar/index?fid=${item.id}`)
  },
  getLuckyStart() {
    utils.showLoading('领取中...')
    userModel.getLuckyStar().then(res => {
      utils.showToast('领取成功,幸运星 +10', 'none')
      let now = new Date()
      let year = now.getFullYear()
      let month = now.getMonth()
      let date = now.getDate()
      let key = `${year}${month}${date}`
      wx.setStorage({
        key: key,
        data: true
      });
      this.setData({
        gotStar: true
      }, () => {
        this.getUserInfo()
        utils.hideAll()
      })
    })
  },
  getMyFansList() {
    let {
      page,
      pageSize
    } = this.data.page1
    let {
      myFans
    } = this.data
    let param = {
      page: page,
      pageSize: pageSize,
      sid: ''
    }
    utils.showLoading()
    fansModel.myFans(param).then(res => {
      let _list = res.list
      myFans = myFans.concat(_list)
      this.computedPage('page1', res.total)
      this.setData({
        myFans: myFans
      })
      utils.hideAll()
    }).catch(() => {
      utils.hideAll()
    })
  },
  getFansGrade() {
    fansModel.fansGrade().then(res => {
      this.setData({
        gradeList: res
      })
    })
  },
  goUrl(e) {
    let url = e.currentTarget.dataset.url
    utils.goUrl(url)
  },
  watch: {

  },
})